package cz.hanis.android.pubaccounting.db;


import cz.hanis.android.pubaccounting.db.dao.ItemDao;
import cz.hanis.android.pubaccounting.db.dao.PubDao;
import cz.hanis.android.pubaccounting.db.tables.ItemTable;
import cz.hanis.android.pubaccounting.db.tables.PubTable;
import cz.hanis.android.pubaccounting.model.Item;
import cz.hanis.android.pubaccounting.model.Pub;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class DbOpenHelper extends SQLiteOpenHelper {
	   	  

	   DbOpenHelper(final Context context) {
	      super(context, DbConstants.DATABASE_NAME, null, DbConstants.DATABASE_VERSION);
	   }

	   @Override
	   public void onOpen(final SQLiteDatabase db) {
	      super.onOpen(db);
	      if (!db.isReadOnly()) {
	         db.execSQL("PRAGMA foreign_keys=ON;");
	         Cursor c = db.rawQuery("PRAGMA foreign_keys", null);
	         if (c.moveToFirst()) {
	            int result = c.getInt(0);
	            Log.i(DbConstants.LOG_TAG, "SQLite foreign key support (1 is on, 0 is off): " + result);
	         } else {
	            Log.i(DbConstants.LOG_TAG, "SQLite foreign key support NOT AVAILABLE");
	         }
	         if (!c.isClosed()) {
	            c.close();
	         }
	      }
	   }

	   @Override
	   public void onCreate(final SQLiteDatabase db) {
	      Log.i(DbConstants.LOG_TAG, "DbOpenHelper onCreate creating database " + DbConstants.DATABASE_NAME);
	      
	      Log.d("DBB_ERR", "1");	      
	      PubTable.onCreate(db);
	      Log.d("DBB_ERR", "2");
	      ItemTable.onCreate(db);
	      Log.d("DBB_ERR", "3");	      	     
	   }

	   
	   
	   @Override
	   public void onUpgrade(final SQLiteDatabase db, final int oldVersion, final int newVersion) {
	      Log.i(DbConstants.LOG_TAG, "SQLiteOpenHelper onUpgrade - oldVersion:" 
	    		  	+ oldVersion + " newVersion:" + newVersion);
	      PubTable.onUpgrade(db, oldVersion, newVersion);
	      ItemTable.onUpgrade(db, oldVersion, newVersion);
	      createExample(db);	      
	   }
	   
	   private void createExample(final SQLiteDatabase db) {
		      PubDao pubDao = new PubDao(db);
		      ItemDao itemDao = new ItemDao(db);	      
		      pubDao.save(new Pub("Skála"));
		      pubDao.save(new Pub("Jam"));		      
		      pubDao.save(new Pub("Pegas"));
		      
		      Pub faval = new Pub("Faval");
		      faval.setHompage("http://www.faval.cz/");
		      pubDao.save(faval);
		      
		      Item i1 = new Item();
		      i1.setName("Gambac 10");
		      i1.setAbbreviation("10");
		      i1.setCost(24);
		      i1.setPubId(1);
		      
		      Item i2 = new Item();
		      i2.setName("Gambac 12");
		      i2.setAbbreviation("12");
		      i2.setCost(21);
		      i2.setPubId(3);

		      Item i3 = new Item();
		      i3.setName("Startky");
		      i3.setAbbreviation("S");
		      i3.setCost(65);
		      i3.setPubId(3);
		      		      
		      itemDao.save(i1);
		      itemDao.save(i2);
		      itemDao.save(i3);		   
	   }

}
